import pickle

import redis


class DefaultConfig(object):
    """
    Flask默认配置
    """
    # flask-sqlalchemy使用的参数
    SQLALCHEMY_DATABASE_URI = ''
    SQLALCHEMY_TRACK_MODIFICATIONS = False
    SQLALCHEMY_ECHO = False
    JWT_SECRET = 'LSJFLSJFLWE23O9UDFNSDF'
    CACHE_TYPE = ''  # 使用redis作为缓存  simple/memcached/uwsgi/redis/filesystem
    # CACHE_KEY_PREFIX  # 设置cache_key的前缀
    CACHE_REDIS_HOST = ''  # redis地址
    CACHE_REDIS_PORT = ''  # redis端口
    # CACHE_REDIS_PASSWORD  # redis密码
    CACHE_REDIS_DB = 2  # 使用哪个数据库
    # CACHE_DIR = 'D:\code\information_items'


class Redis:
    @staticmethod
    def connect():
        r = redis.StrictRedis(host='', port=6379)
        return r

    # 将内存数据二进制通过序列号转为文本流，再存入redis
    @staticmethod
    def set_data(r, key, data, ex=None):
        r.set(key, pickle.dumps(data), ex)

    # 将文本流从redis中读取并反序列化，返回返回
    @staticmethod
    def get_data(r, key):
        data = r.get(key)
        if data is None:
            return None
        return pickle.loads(data)
